package Utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Array;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

//接口请求工具类
public class RestTemplateUtils
{
    /*
        带参数的Get请求
        @param URL 请求URL
        @param param 请求参数
        @return String
     */
    public static String doGet(String URL, Map<String,String> params)
    {
        //创建RestTemplatet对象
        RestTemplate restTemplate = new RestTemplate();
        //结果字符串
        String ResultString = "";
        try{
            //构建URI
            URIBuilder uriBuilder = new URIBuilder(URL);
            //添加参数
            //如果参数非空
            if(params != null)
            {
                for(String Key: params.keySet())
                {
                    uriBuilder.addParameter(Key,params.get(Key));
                }
            }
            URI uri = uriBuilder.build();
            //进行请求访问
            ResultString = restTemplate.getForObject(uri,String.class);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultString;
    }
}
